﻿
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Zocalo

    'eventos
    Public Event DataArrival(ByVal Value As String)
    Public Event [Event](ByVal Value As String)
    Public Event [Error](ByVal Value As String)

    'propiedades
    Private vRemoteHost As String
    Private vRemotePort As Integer
    Private vState As StateType

    'enumeraciones
    Public Enum StateType
        Disconnected = 0
        Connected = 1
    End Enum

    'objeto del tipo Socket TCP
    Private objSocket As New Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Private readThread As System.Threading.Thread

    'variables locales
    Private bytes() As Byte

    'delegados
    Private Delegate Sub delReceiveData()
    Private dReceiveData As New delReceiveData(AddressOf ReceiveData)
    Private Delegate Sub delStartThread()
    Private dStartThread As New delStartThread(AddressOf StartThread)

    Private Sub StartThread()
        Try
            readThread = New System.Threading.Thread(AddressOf dReceiveData.Invoke)
            With readThread
                .IsBackground = True
                .Start()
            End With
        Catch ex As Exception

        End Try
    End Sub

    Public ReadOnly Property State() As StateType
        Get
            Return vState
        End Get
    End Property

    Public Property RemoteHost() As String
        Get
            Return vRemoteHost
        End Get
        Set(ByVal Value As String)
            vRemoteHost = Value
        End Set
    End Property

    Public Property RemotePort() As String
        Get
            Return vRemotePort
        End Get
        Set(ByVal Value As String)
            vRemotePort = Value
        End Set
    End Property

    Public Sub Connect()
        Try
            Dim ipAddress As IPAddress = System.Net.IPAddress.Parse(vRemoteHost)
            Dim remoteEP As New IPEndPoint(ipAddress, vRemotePort)
            Try
                objSocket.Connect(remoteEP)
                dStartThread.Invoke()
                RaiseEvent Event("Socket Connected - " & objSocket.RemoteEndPoint.ToString())
            Catch ane As ArgumentNullException
                RaiseEvent Error("Argument Null Exception - " & ane.ToString())
            Catch se As SocketException
                RaiseEvent Error("Socket Exception - " & se.ToString())
            Catch e As Exception
                RaiseEvent Error("Unexpected exception - " & e.ToString())
            End Try
        Catch ex As Exception
            RaiseEvent Error(ex.ToString)
        End Try
    End Sub

    Public Sub SendData(ByVal Value As String)
        Try
            Dim msg As Byte() = Encoding.ASCII.GetBytes(Value)
            Dim bytesSent As Integer = objSocket.Send(msg)
        Catch ex As Exception
            RaiseEvent Error(ex.ToString)
        End Try
    End Sub

    Public Sub ReceiveData()
        Do
            Application.DoEvents()
            Try
                Dim vBytesCola As Integer = objSocket.Available
                If vBytesCola > 0 Then
                    ReDim bytes(vBytesCola)
                    Dim bytesRec As Integer = objSocket.Receive(bytes)
                    Dim DataRec As String = Encoding.ASCII.GetString(bytes, 0, bytesRec)
                    RaiseEvent DataArrival(DataRec)
                End If
            Catch ex As Exception
                RaiseEvent Error(ex.ToString)
            End Try
        Loop
    End Sub

    Public Sub Close()
        Try
            objSocket.Shutdown(SocketShutdown.Both)
            objSocket.Close()
            RaiseEvent Event("Socket Disconnected")
        Catch ex As Exception
            RaiseEvent Error(ex.ToString)
        End Try
    End Sub

    Protected Overrides Sub Finalize()
        Try
            Call Close()
            MyBase.Finalize()
        Catch ex As Exception
            RaiseEvent Error(ex.ToString)
        End Try
    End Sub

End Class

